package org.hibernate.boot.spi;

import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.dialect.SqliteDialect;
import org.hibernate.engine.jdbc.dialect.internal.DialectResolverSet;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolver;

public class SqliteMetadataBuilderInitializer implements MetadataBuilderInitializer {

	@Override
	public void contribute(MetadataBuilder metadataBuilder, StandardServiceRegistry serviceRegistry) {
		DialectResolver dialectResolver = serviceRegistry.getService(DialectResolver.class);

		if (!(dialectResolver instanceof DialectResolverSet)) {
			return;
		}

		((DialectResolverSet) dialectResolver).addResolver(resolver);
	}

	private static final SqliteDialect dialect = new SqliteDialect();
	
	private static final DialectResolver resolver = (DialectResolver) info -> {
        if (info.getDatabaseName().equalsIgnoreCase("SQLite")) {
            return dialect;
        }

        return null;
    };
}
